package com.token.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @auther: henriy
 * @Date: 2023/11/1
 */
public class Utils {
    public static void main(String[] args) {
        String input = "The current time is ${yyyy-MM-dd HH:mm:ss}.";
        String regex = "\\$\\{yyyy-MM-dd HH:mm:ss\\}"; // 匹配表达式

        // 创建Pattern对象，并指定正则表达式
        Pattern pattern = Pattern.compile(regex);

        // 创建Matcher对象，并将输入字符串与Pattern进行匹配
        Matcher matcher = pattern.matcher(input);

        // 使用replaceAll方法替换匹配到的表达式为当前时间
        String replaced = matcher.replaceAll(getCurrentDateTime());

        System.out.println("原始字符串：" + input);
        System.out.println("替换后的字符串：" + replaced);
    }

    private static String getCurrentDateTime() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return now.format(formatter);
    }

    public static LocalDateTime parse(String str, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(str, df);
    }

}
